package com.gxa.controller;

import com.gxa.common.Constants;
import com.gxa.common.ResponseData;
import com.gxa.pojo.*;
import com.gxa.service.GoodsService;
import com.gxa.service.IooService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *商品控制类
 * @author bq
 * Date: 2020/10/27 10:44
 * Description:
 * Version: V1.0
 */
@Controller
@RequestMapping("goods")
@Api(value = "商品数据",tags = "商品数据接口API")
public class GoodsController {
    @Autowired
    private ResponseData responseData;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private IooService iooService;


    /**
     * 根据主键修改库存单状态
     * @param ioo
     * @return
     */
    @PostMapping("updateIooStateByIooId")
    @ApiOperation(value = "根据主键修改库存单状态",notes = "根据主键修改库存单状态接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iooId",value = "主键",paramType = "query",required = true),
            @ApiImplicitParam(name = "iooState",value = "状态",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData updateIooStateByIooId(@RequestBody Ioo ioo){
        System.out.println("根据主键修改库存单状态ioo="+ioo);
        int result=iooService.updateIoo(ioo);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("修改失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据主键删除库存单
     * @param iooId
     * @return
     */
    @GetMapping("deleteIooByIooId")
    @ApiOperation(value = "根据主键删除库存单",notes = "根据主键删除库存单接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=400,message="删除失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iooId",value = "主键",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData deleteIooByIooId(int iooId){
        System.out.println("根据主键删除库存单iooId="+iooId);
        int result=iooService.deleteIooByIooId(iooId);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("删除失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据主键修改库存单状态并添加驳回信息
     * @param ioo
     * @return
     */
    @PostMapping("updateIooStateAndIooRfrByIooId")
    @ApiOperation(value = "根据主键修改库存单状态并添加驳回信息",notes = "根据主键修改库存单状态接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iooId",value = "主键",paramType = "query",required = true),
            @ApiImplicitParam(name = "iooState",value = "状态",paramType = "query",required = true),
            @ApiImplicitParam(name = "iooRfr",value = "驳回原因",paramType = "query")
    })
    @ResponseBody
    public ResponseData updateIooStateAndIooRfrByIooId(@RequestBody Ioo ioo){
        System.out.println("根据主键修改库存单状态并添加驳回信息ioo="+ioo);
        int result=iooService.updateIooStateAndIooRfrByIooId(ioo);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("修改失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据提交信息修改库存操作
     * @param ioo
     * @return
     */
    @PostMapping("updateIoo")
    @ApiOperation(value = "根据提交信息修改库存操作",notes = "根据提交信息修改库存操作接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData updateIoo(@RequestBody @ApiParam(name = "ioo",value = "库存单信息",required = true)Ioo ioo){
        System.out.println("根据提交信息修改库存操作"+ioo);
        int result=iooService.updateIoo(ioo);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("修改失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据商品编码查询库存
     * @param goodsCode
     * @return
     */
    @GetMapping("queryBlurryGoods")
    @ApiOperation(value = "根据商品编码查询库存",notes = "根据商品编码查询库存接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsCode",value = "商品编码",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData queryBlurryGoods(String goodsCode){
        System.out.println("根据商品编码查询库存 goodsCode="+goodsCode);
        Goods goods=goodsService.queryBlurryGoods(goodsCode);
        if (goods==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("无数据");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(goods);
        }
        return responseData;
    }

    /**
     * 模糊查询库存单
     * @param ioo
     * @return
     */
    @PostMapping(value = "queryBlurryIoo",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "模糊查询库存单",notes = "模糊查询库存单接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData queryBlurryIoo(@RequestBody @ApiParam(name = "ioo",value = "库存单信息",required = true)Ioo ioo){
        System.out.println("模糊查询库存单 ioo="+ioo);
        List<Ioo> iooList=iooService.queryBlurryIoo(ioo);
        System.out.println("iooList="+iooList);
        if(iooList==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("无数据");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(iooList);
        }
        return responseData;
    }

    /**
     * 根据提交信息完成入库操作
     * @param ioos
     * @return
     */
    @PostMapping("addIoos")
    @ApiOperation(value = "根据提交信息完成入库操作",notes = "根据提交信息完成入库操作接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=400,message="添加失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData addIoos(@RequestBody @ApiParam(name = "ioo",value = "库存单信息",required = true)List<Ioo> ioos) throws Exception {
        System.out.println("根据提交信息完成入库操作 ioos="+ioos);
        int result=iooService.addIoos(ioos);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("添加失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据库存单状态查询库存单
     * @param iooState
     * @return
     */
    @GetMapping("queryIoosByIooState")
    @ApiOperation(value = "根据库存单状态查询库存单",notes = "根据库存单状态查询库存单接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iooState",value = "状态",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData queryIooByIooState(int iooState){
        System.out.println("根据库存单状态查询库存单=iooState="+iooState);
        List<Ioo> iooList=iooService.queryIooByIooState(iooState);
        if (iooList==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("无数据");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(iooList);
        }
        return responseData;
    }
    /**
     * 根据商品主键查询商品信息
     * @param goodsId
     * @return
     */
    @GetMapping("selectGoodsByGoodsId")
    @ApiOperation(value = "根据商品主键查询商品信息",notes = "根据商品主键查询商品信息接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品主键",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData selectGoodsByGoodsId(int goodsId){
        System.out.println("根据商品主键查询商品信息 goodsId="+goodsId);
        Goods goods = goodsService.queryById(goodsId);
        if (goods==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("查询失败");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(goods);
        }
        return responseData;
    }
    /**
     * 根据商品主键删除商品
     * @param goodsId
     * @return
     */
    @GetMapping("deleteGoodsByGoodsId")
    @ApiOperation(value = "根据商品主键删除商品",notes = "根据商品主键删除商品接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=400,message="删除失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品主键",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData deleteGoodsByGoodsId(int goodsId){
        System.out.println("根据商品主键删除商品 goodsId="+goodsId);
        int result=goodsService.deleteGoodsByGoodsId(goodsId);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("删除失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 根据提交信息修改商品信息
     * @param goods
     * @return
     */
    @PostMapping("updateGoods")
    @ApiOperation(value = "根据提交信息修改商品信息",notes = "根据提交信息修改商品信息接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData updateGoods(@RequestBody @ApiParam(name = "goods",value = "商品信息信息",required = true)Goods goods){
        System.out.println("根据提交信息修改商品信息 goods="+goods);
        int result=goodsService.updateGoods(goods);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("修改失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据单据编号查询库存单
     * @param bill
     * @return
     */
    @GetMapping("selectIooByBill")
    @ApiOperation(value = "根据单据编号查询库存单",notes = "根据单据编号查询库存单接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData selectIooByIooId(String bill){
        System.out.println("根据单据编号查询库存单 bill="+bill);
        Ioo ioo = iooService.queryIooByBill(bill);
        if (ioo==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("查询失败");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(ioo);
        }
        return responseData;
    }
    /**
     * 根据iooId查询库存单
     * @param Iooid
     * @return
     */
    @GetMapping("selectIooByIooId")
    @ApiOperation(value = "根据iooId查询库存单",notes = "根据iooId查询库存单接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData selectIooByIooId(int Iooid){
        System.out.println("根据单据编号查询库存单 Iooid="+Iooid);
        Ioo ioo = iooService.queryIooByIooid(Iooid);
        if (ioo==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("查询失败");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(ioo);
        }
        return responseData;
    }
    /**
     * 查询所有库存单不分页
     * @return
     */
    @GetMapping("selectIoo")
    @ApiOperation(value = "查询所有库存单不分页",notes = "查询所有库存单不分页接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeState",value = "库存单类型",paramType = "query")
    })
    @ResponseBody
    public ResponseData selectIoo(@RequestParam(defaultValue = "-1") int typeState){
        System.out.println("查询所有库存单不分页 typeState="+typeState);
        List<Ioo> iooList=null;
        if (typeState==-1){
            iooList = iooService.queryIoo();
        }else {
            iooList = iooService.queryIooByTypeState(typeState);
        }
        if (iooList==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("无数据");
            responseData.setData(null);
        }else{
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(iooList);
        }
        return responseData;
    }
    /**
     * 库存统计不分页
     * @return
     */
    @GetMapping("selectGoods")
    @ApiOperation(value = "库存统计不分页",notes = "库存统计不分页接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData selectGoods(){
        System.out.println("库存统计不分页");
        List<Goods> goodsList=goodsService.selectGoods();
        if (goodsList==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("无数据");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(goodsList);
        }
        return responseData;
    }
    /**
     * 单个入库操作
     * @param ioo
     * @return
     */
    @PostMapping(value = "insertIoo",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "单个入库操作",notes = "单个入库操作操作接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=400,message="添加失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData addIoos(@RequestBody @ApiParam(name = "ioo",value = "库存单信息",required = true)Ioo ioo) throws Exception {
        System.out.println("单个入库操作 ioo="+ioo);
        int result=iooService.addIoo(ioo);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("添加失败");
            responseData.setData(null);
        }
        return responseData;
    }
}
